/*
Copyright (c) [2019年5月1日] [吴超]
[MBT_Studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
		 http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#include "插件.h"

#include "A_引擎/引擎.h"
#include "扩展插件.h"

#include "A_引擎/文件工程/插件模板.h"
#include "框架/工作区/节点视口/节点构建.h"


#include <系统/系统平台.h>





bool f_插件_加载(S_插件& 插件) {
	
	std::filesystem::path 插件文件;

#ifdef _DEBUG
	插件文件 = S_框架::m_库默认路径 / u"插件模块/Debug" / (插件.m_Name + u".dll");
#else
	插件文件 = S_框架::m_库默认路径 / u"插件模块" / (插件.m_Name + u".dll");
#endif // DEBUG
	

	插件.m_库 = f_sys_加载代码(插件文件.string(), E_JIT方式::e_jit_dll);
	if(!插件.m_库) return false;
	
	uint64 函数指针 = f_sys_取函数指针(S_节点数据::g插件库[插件.m_Name]->m_库, "f_plugin_import", E_JIT方式::e_jit_dll);
	if (!函数指针) return false;

	if (函数指针) {
		auto 加载函数 = (f_plugin_加载卸载函数指针)函数指针;
		加载函数(插件.m_模块);
		//插件.m_模块.clear();
		for (auto& e : 插件.m_模块) {
			switch (e.second.m_Type) {
				case E_扩展类型::e_扩展_节点: {
					f_节点构建_注册扩展节点(E_节点树类型::e_type_物体节点树, e.second);
					f_node_绑定节点扩展(e.first, f_nodePlugin_节点构建参数(e.second));

					f_节点面板构建_重构扩展节点(e.second);
					
					break;
				}
				case E_扩展类型::e_扩展_物体构建: {
					
					auto* 构建参数 = f_nodePlugin_物体构建参数(e.second);
					switch (e.second.m_注册工作区) {
						case E_工作区类型::e_后台: {
							break;
						}
						default:
							break;
					}

					S_插件创建物体 创建物体;
					创建物体.mf_物体创建 = 构建参数->mf_物体创建;
					创建物体.mf_物体销毁 = 构建参数->mf_物体销毁;
					创建物体.mf_物体选择 = 构建参数->mf_物体选择;
					创建物体.mf_物体更新 = 构建参数->mf_物体更新;
					创建物体.m_拾取 = 构建参数->m_拾取;
					创建物体.m_渲染 = 构建参数->m_渲染;
					创建物体.m_可操作 = 构建参数->m_可操作;

					S_框架::g_插件后端物体[e.first] = 创建物体;
					break;
				}
				case E_扩展类型::e_扩展_变换操作: {
					S_框架::g_插件_变换操作[e.first] = *f_nodePlugin_变换操作构建参数(e.second);
					break;
				}
				case E_扩展类型::e_扩展_场景更新: {
					std::cout<<"还未完成\n";
					break;
				}
				default:
					break;
			}
		}
	}
	
	return true;
}

bool f_插件_卸载(S_插件& 插件) {
	
	uint64 函数指针 = f_JIT_取函数指针(插件.m_库, "f_plugin_uninstall", E_JIT方式::e_jit_dll);
	if (!函数指针) return false;


	auto 卸载函数 = (f_plugin_加载卸载函数指针)函数指针;
	卸载函数(插件.m_模块);


	for (auto& e : 插件.m_模块) {
		switch (e.second.m_Type) {
			case E_扩展类型::e_扩展_节点: {
				
				f_node_卸载扩展节点(e.first);
				f_节点插件_销毁节点模块构建参数(e.second);

				break;
			}
			case E_扩展类型::e_扩展_物体构建: {
				
				break;
			}
			case E_扩展类型::e_扩展_变换操作: {

				break;
			}
			case E_扩展类型::e_扩展_场景更新: {
				
				break;
			}
			default:
				break;
		}
	}

	插件.m_模块.clear();

	f_JIT_销毁(插件.m_库, E_JIT方式::e_jit_dll);
	插件.m_库 = 0;
	
	return true;
}

void f_插件_重载(S_插件& 插件) {
	f_插件_加载(插件);
	f_插件_卸载(插件);
}

void f_插件_构建工程(const std::filesystem::path& path, const std::u16string& 插件名称) {
	
	auto 插件模块路径 = S_框架::m_扩展模块源码路径 / 插件名称 / (u"CmakeLists.txt");

	auto 构建目录 = S_框架::m_扩展模块源码路径 / 插件名称;
	f_file_创建目录(构建目录);

	auto 源码路径 = 构建目录 / "src";
	f_file_创建目录(源码路径);

	auto 自定义cmake文件 = 源码路径 / u"自定义配置.cmake";

	auto 插件模板路径 = S_框架::m_扩展模块源码路径 / 插件名称 / (u"src/main.cpp");
	FILE* f = nullptr;
	if (f_file_文件是否存在(插件模板路径) == false) {
		auto err = fopen_s(&f, 插件模板路径.string().c_str(), "w");
		if (f) {
			auto cmakeCode = f_插件模板_主文件();
			auto txt = f_str_u16_to_u8(cmakeCode);
			fwrite(txt.data(), sizeof(uint8), txt.size(), f);
			fclose(f);
		}
	}



	f = nullptr;
	auto err = fopen_s(&f, 插件模块路径.string().c_str(), "w");
	if (f) {
		auto 程序路径 = S_引擎配置::g_程序路径.generic_u16string();

		auto debug程序目录 = S_引擎配置::g_程序路径.parent_path().generic_u16string() + u"/debug/";
		auto release程序目录 = S_引擎配置::g_程序路径.parent_path().generic_u16string() + u"/release/";



		std::u16string cmakeCode = u"cmake_minimum_required(VERSION 3.29)\n";
		cmakeCode += u"project(" + 插件名称 + u" LANGUAGES CXX)\n";
		cmakeCode += u"set(CMAKE_C_STANDARD 17)\n";
		cmakeCode += u"set(CMAKE_CXX_STANDARD 17)\n";
		cmakeCode += u"set(CMAKE_CXX_STANDARD_REQUIRED ON)\n";
		//cmakeCode += u"include_directories(include)\n";
		//cmakeCode += u"link_directories(path_to_library_dir)\n";

		cmakeCode += u"if(MSVC)\n";
		//多线程编译
		cmakeCode += u"set(CMAKE_C_FLAGS \"${CMAKE_C_FLAGS} /MP\")\n";
		cmakeCode += u"set(CMAKE_CXX_FLAGS \"${CMAKE_CXX_FLAGS} /MP\")\n";

		cmakeCode += u"set(CMAKE_C_FLAGS \"${CMAKE_C_FLAGS} /Qpar\")\n";
		cmakeCode += u"set(CMAKE_CXX_FLAGS \"${CMAKE_CXX_FLAGS} /Qpar\")\n";
		cmakeCode += u"set(CMAKE_C_FLAGS \"${CMAKE_C_FLAGS} /arch:AVX2\")\n";
		cmakeCode += u"set(CMAKE_CXX_FLAGS \"${CMAKE_CXX_FLAGS} /arch:AVX2\")\n";
		cmakeCode += u"endif()\n";


		//NuGet包
		//cmakeCode += u"set(NUGET_PACKAGES_DIR \"${CMAKE_SOURCE_DIR}/packages\")\n";
		//cmakeCode += u"set(MY_NUGET_PACKAGE_NAME \"boost_locale-vc142\")\n";
		//cmakeCode += u"set(MY_NUGET_PACKAGE_VERSION \"1.84.0\")\n";
		//cmakeCode += u"set(MY_NUGET_PACKAGE_DIR \"${NUGET_PACKAGES_DIR}/${MY_NUGET_PACKAGE_NAME}.${MY_NUGET_PACKAGE_VERSION}\")\n";



		cmakeCode += u"set(INCLUDE_STD_PATH \"" + release程序目录 + u"include/\")\n";
		cmakeCode += u"set(LIB_STD_PATH \"" + release程序目录 + u"lib/\")\n";
		cmakeCode += u"set(INCLUDE_STD_PATH_D \"" + debug程序目录 + u"include/\")\n";
		cmakeCode += u"set(LIB_STD_PATH_D \"" + debug程序目录 + u"lib/\")\n";

		//cmakeCode += u"set(LIB_PATH_DEBUG \"" + 程序路径 + u"/lib/debug/\")\n";
		//cmakeCode += u"set(LIB_PATH_RELEASE \"" + 程序路径 + u"/lib/\")\n";
		


		if (f_file_文件是否存在(自定义cmake文件)) {
			cmakeCode += u"include(\"" + 自定义cmake文件.generic_u16string() + u"\")\n";
		}
		cmakeCode += u"get_cmake_property(VARS VARIABLES)\n";
		//cmakeCode += u"set(HEADERS_PATH \"include\")\n";
		//cmakeCode += u"set(LIB_PATH \"lib\")\n";

		cmakeCode += u"if (CMAKE_BUILD_TYPE STREQUAL \"Debug\")\n";
		cmakeCode += u"set(CUSTOM_HEADERS_ \"CUSTOM_HEADERS_DEBUG_\")\n";
		cmakeCode += u"set(CUSTOM_LIB_ \"CUSTOM_LIB_DEBUG_\")\n";
		cmakeCode += u"else()\n";
		cmakeCode += u"set(CUSTOM_HEADERS_ \"CUSTOM_HEADERS_RELEASE_\")\n";
		cmakeCode += u"set(CUSTOM_LIB_ \"CUSTOM_LIB_RELEASE_\")\n";
		cmakeCode += u"endif()\n";


		cmakeCode += u"foreach(VAR ${VARS})\n";
		cmakeCode += u"	if (VAR MATCHES ${CUSTOM_HEADERS_})\n";
		cmakeCode += u"		list(APPEND INCLUDE_STD_PATH \"${${VAR}}\")\n";
		cmakeCode += u"		list(APPEND INCLUDE_STD_PATH_D \"${${VAR}}\")\n";
		cmakeCode += u"	endif()\n";
		cmakeCode += u"	if (VAR MATCHES ${CUSTOM_LIB_})\n";
		cmakeCode += u"		list(APPEND LIB_STD_PATH \"${${VAR}}\")\n";
		cmakeCode += u"		list(APPEND LIB_STD_PATH_D \"${${VAR}}\")\n";
		cmakeCode += u"		endif()\n";
		cmakeCode += u"endforeach()\n";

		//cmakeCode += u"list(APPEND ${HEADERS_PATH} \"${INCLUDE_STD_PATH}\")\n";
		//cmakeCode += u"list(APPEND ${LIB_PATH} \"${LIB_STD_PATH}\")\n";
		//cmakeCode += u"list(APPEND LIB_PATH \"${MY_NUGET_PACKAGE_DIR}/lib\")\n";
		cmakeCode += u"include_directories(\n";
		cmakeCode += u"$<$<CONFIG:Debug>:${HEADERS_PATH_D}>\n";
		cmakeCode += u"$<$<NOT:$<CONFIG:Debug>>:${HEADERS_PATH}>\n";
		cmakeCode += u")\n";
		cmakeCode += u"link_directories(\n";
		cmakeCode += u"$<$<CONFIG:Debug>:${LIB_PATH_D}>\n";
		cmakeCode += u"$<$<NOT:$<CONFIG:Debug>>:${LIB_PATH}>\n";
		cmakeCode += u")\n";
		//cmakeCode += u"include_directories(${HEADERS_PATH})\n";
		//cmakeCode += u"link_directories(${LIB_PATH})\n";

		cmakeCode += u"include_directories(\n";
		cmakeCode += u"$<$<CONFIG:Debug>:${INCLUDE_STD_PATH_D}>\n";
		cmakeCode += u"$<$<NOT:$<CONFIG:Debug>>:${INCLUDE_STD_PATH}>\n";
		cmakeCode += u")\n";
		cmakeCode += u"link_directories(\n";
		cmakeCode += u"$<$<CONFIG:Debug>:${LIB_STD_PATH_D}>\n";
		cmakeCode += u"$<$<NOT:$<CONFIG:Debug>>:${LIB_STD_PATH}>\n";
		cmakeCode += u")\n";
		//cmakeCode += u"include_directories(${INCLUDE_STD_PATH})\n";
		//cmakeCode += u"link_directories(${LIB_STD_PATH})\n";

		//cmakeCode += u"link_directories(\n";
		//cmakeCode +=	u"$<IF:$<CONFIG:Debug>, ${LIB_PATH_DEBUG}, ${LIB_PATH_RELEASE}>\n";
		//cmakeCode += u")\n";


		cmakeCode += u"file(GLOB_RECURSE SOURCES\n";
		cmakeCode += u"	\"${CMAKE_SOURCE_DIR}/src/*.cpp\"\n";
		cmakeCode += u"	\"${CMAKE_SOURCE_DIR}/src/*.h\"\n";
		cmakeCode += u"	\"${CMAKE_SOURCE_DIR}/src/*.hpp\"\n";
		cmakeCode += u")\n";
		//cmakeCode += u"set(HEADERS_PATH \"" + S_引擎配置::g_程序路径.u16string() + u"\"/include\")\n";
		//cmakeCode += u"file(GLOB SOURCES \"./src/a.cpp\")\n";

		cmakeCode += u"set(DLL_NAME \"newPlugin\")\n";
		//cmakeCode += u"add_library(${DLL_NAME} SHARED ${SOURCES} ${HEADERS})\n";
		cmakeCode += u"add_library(${DLL_NAME} SHARED ${SOURCES} )\n";


		cmakeCode += u"file(GLOB LIBRARIES \"${LIB_PATH}/*.lib\")\n";
		cmakeCode += u"target_link_libraries(${DLL_NAME} ${LIBRARIES})\n";

		cmakeCode += u"set(LIBS\n";
		cmakeCode += u"	基础库.lib\n";
		cmakeCode += u"	图形库.lib\n";
		cmakeCode += u"	数据工具库.lib\n";
		cmakeCode += u"	engine.lib\n";
		cmakeCode += u"	JIT.lib\n";
		cmakeCode += u"	节点.lib\n";
		cmakeCode += u"	基础数据.lib\n";
		cmakeCode += u"	节点数据交互.lib\n";
		cmakeCode += u")\n";
		cmakeCode += u"target_link_libraries(${DLL_NAME} ${LIBS})\n";
		//cmakeCode += u"foreach(LIBRARY ${LIBRARIES})\n";
		//	cmakeCode += u"	target_link_libraries(MyExecutable \"${LIBRARY}\")\n";
		//cmakeCode += u"endforeach()\n";
		//cmakeCode += u"set_target_properties(${DLL_NAME} PROPERTIES COMPILE_FLAGS \"/MTd\" )\n";
		//cmakeCode += u"set_target_properties(${DLL_NAME} PRIVATE \n";
		//cmakeCode += u"	$<$<CONFIG:Debug>:/MTd>\n";
		//cmakeCode += u"	$<$<NOT:$<CONFIG:Debug>>:/MT>\n";
		//cmakeCode += u"set_target_properties(${DLL_NAME} PROPERTIES COMPILE_FLAGS $<$<CONFIG:Debug>:/MTd>$<$<NOT:$<CONFIG:Debug>>:/MT )\n";
		//cmakeCode += u"set_target_properties(${DLL_NAME} PROPERTIES \n";
		//cmakeCode += u"	$<$<CONFIG:Debug>:/MTd>\n";
		//cmakeCode += u"	$<$<NOT:$<CONFIG:Debug>>:/MT>\n";
		//cmakeCode += u"STATIC_LIBRARY_FLAGS_DEBUG \"/MTd\"\n";
		//cmakeCode += u"SHARED_LIBRARY_FLAGS_RELEASE \"/MT\"\n";
		//cmakeCode += u")\n";
		//cmakeCode += u"target_include_directories(newPlugin PRIVATE ${HEADERS_PATH})\n";
		//cmakeCode += u"target_link_libraries(newPlugin PRIVATE ${LIB_PATH})\n";

		//设置MD 还是 MT
		//cmakeCode += u"target_compile_options(${DLL_NAME} PRIVATE\n";
		//cmakeCode += u"	$<$<CONFIG:Debug>:/MTd>\n";
		//cmakeCode += u"	$<$<NOT:$<CONFIG:Debug>>:/MT> \n";
		//cmakeCode += u")\n";

		//忽略特定默认库
		cmakeCode += u"target_link_options(${DLL_NAME} PRIVATE\n";
		//cmakeCode += u"	/NODEFAULTLIB:libboost_locale-vc142-mt-sgd-x64-1_85.lib\n";
		//cmakeCode += u"	/NODEFAULTLIB:libboost_locale-vc142-mt-s-x64-1_85.lib\n";
		//cmakeCode += u"	/NODEFAULTLIB:libfbxsdk-mt.lib\n";
		//cmakeCode += u"	/NODEFAULTLIB:LLVMCore.lib\n";
		//cmakeCode += u"	/NODEFAULTLIB:LLVMExecutionEngine.lib\n";
		//cmakeCode += u"	/NODEFAULTLIB:LLVMInterpreter.lib\n";
		//cmakeCode += u"	/NODEFAULTLIB:LLVMMC.lib\n";
		//cmakeCode += u"	/NODEFAULTLIB:LLVMMCJIT.lib\n";
		//cmakeCode += u"	/NODEFAULTLIB:LLVMSupport.lib\n";
		//cmakeCode += u"	/NODEFAULTLIB:LLVMX86CodeGen.lib\n";
		//cmakeCode += u"	/NODEFAULTLIB:LLVMX86Desc.lib\n";
		//cmakeCode += u"	/NODEFAULTLIB:LLVMX86Info.lib\n";
		//cmakeCode += u"	/NODEFAULTLIB:LLVMOrcTargetProcess.lib\n";
		//cmakeCode += u"	/NODEFAULTLIB:LLVMOrcShared.lib\n";
		//cmakeCode += u"	/NODEFAULTLIB:LLVMRuntimeDyld.lib\n";
		//cmakeCode += u"	/NODEFAULTLIB:LLVMMCDisassembler.lib\n";
		//cmakeCode += u"	/NODEFAULTLIB:LLVMAsmPrinter.lib\n";
		//cmakeCode += u"	/NODEFAULTLIB:LLVMDebugInfoDWARF.lib\n";
		//cmakeCode += u"	/NODEFAULTLIB:LLVMDebugInfoMSF.lib\n";
		//cmakeCode += u"	/NODEFAULTLIB:LLVMInstrumentation.lib\n";
		//cmakeCode += u"	/NODEFAULTLIB:LLVMGlobalISel.lib\n";
		//cmakeCode += u"	/NODEFAULTLIB:LLVMSelectionDAG.lib\n";
		//cmakeCode += u"	/NODEFAULTLIB:LLVMCodeGen.lib\n";
		//cmakeCode += u"	/NODEFAULTLIB:LLVMTarget.lib\n";
		//cmakeCode += u"	/NODEFAULTLIB:LLVMBitWriter.lib\n";
		//cmakeCode += u"	/NODEFAULTLIB:LLVMScalarOpts.lib\n";
		//cmakeCode += u"	/NODEFAULTLIB:LLVMAggressiveInstCombine.lib\n";
		//cmakeCode += u"	/NODEFAULTLIB:LLVMInstCombine.lib\n";
		//cmakeCode += u"	/NODEFAULTLIB:LLVMTransformUtils.lib\n";
		//cmakeCode += u"	/NODEFAULTLIB:LLVMAnalysis.lib\n";
		//cmakeCode += u"	/NODEFAULTLIB:LLVMObject.lib\n";
		//cmakeCode += u"	/NODEFAULTLIB:LLVMBitReader.lib\n";
		//cmakeCode += u"	/NODEFAULTLIB:LLVMMCParser.lib\n";
		//cmakeCode += u"	/NODEFAULTLIB:LLVMDebugInfoCodeView.lib\n";
		//cmakeCode += u"	/NODEFAULTLIB:LLVMTextAPI.lib\n";
		//cmakeCode += u"	/NODEFAULTLIB:LLVMProfileData.lib\n";
		//cmakeCode += u"	/NODEFAULTLIB:LLVMCFGuard.lib\n";
		//cmakeCode += u"	/NODEFAULTLIB:LLVMBinaryFormat.lib\n";
		//cmakeCode += u"	/NODEFAULTLIB:LLVMRemarks.lib\n";
		//cmakeCode += u"	/NODEFAULTLIB:LLVMBitstreamReader.lib\n";
		//cmakeCode += u"	/NODEFAULTLIB:LLVMOrcJIT.lib\n";
		//cmakeCode += u"	/NODEFAULTLIB:LLVMX86AsmParser.lib\n";
		//cmakeCode += u"	/NODEFAULTLIB:LLVMX86Disassembler.lib\n";
		//cmakeCode += u"	/NODEFAULTLIB:LLVMPasses.lib\n";
		//cmakeCode += u"	/NODEFAULTLIB:LLVMCoroutines.lib\n";
		//cmakeCode += u"	/NODEFAULTLIB:LLVMipo.lib\n";
		//cmakeCode += u"	/NODEFAULTLIB:LLVMFrontendOpenMP.lib\n";
		//cmakeCode += u"	/NODEFAULTLIB:LLVMIRReader.lib\n";
		//cmakeCode += u"	/NODEFAULTLIB:LLVMAsmParser.lib\n";
		//cmakeCode += u"	/NODEFAULTLIB:LLVMLinker.lib\n";
		//cmakeCode += u"	/NODEFAULTLIB:LLVMObjCARCOpts.lib\n";
		//cmakeCode += u"	/NODEFAULTLIB:LLVMVectorize.lib\n";
		//cmakeCode += u"	/NODEFAULTLIB:LLVMJITLink.lib\n";
		//cmakeCode += u"	/NODEFAULTLIB:LLVMDemangle.lib\n";
		cmakeCode += u"	/NODEFAULTLIB:zlibd.lib\n";
		cmakeCode += u"	/NODEFAULTLIB:zlibstaticd.lib\n";
		cmakeCode += u"	/NODEFAULTLIB:zlib.lib\n";
		cmakeCode += u"	/NODEFAULTLIB:zlibstatic.lib\n";
		cmakeCode += u")\n";


		cmakeCode += u"foreach(SOURCE_FILE ${SOURCES})\n";
		cmakeCode += u"	file(RELATIVE_PATH SOURCE_FILE_REL \"${CMAKE_SOURCE_DIR}/\" \"${SOURCE_FILE}\")\n";
		cmakeCode += u"	string(REGEX REPLACE \"/[^/]*$\" \"\" SOURCE_FILE_REL \"${SOURCE_FILE_REL}\")\n";
		cmakeCode += u"	string(REPLACE \"/\" \"\\\\\" SOURCE_FILE_GROUP \"${SOURCE_FILE_REL}\")\n";
		cmakeCode += u"	source_group(\"${SOURCE_FILE_GROUP}\" FILES \"${SOURCE_FILE}\")\n";
		cmakeCode += u"endforeach()\n";



		cmakeCode += u"target_compile_definitions(${DLL_NAME} PRIVATE MYDLL_EXPORTS)\n";


		//auto 输出目录 = S_框架::m_库默认路径 / u"插件模块";

		cmakeCode += u"set_target_properties(${DLL_NAME} PROPERTIES\n";
		cmakeCode += u"	OUTPUT_NAME  " + 插件名称 + u"\n";
		//cmakeCode += u"	RUNTIME_OUTPUT_DIRECTORY_DEBUG \"${CMAKE_BINARY_DIR}/bin/debug\"\n";
		//cmakeCode += u"	RUNTIME_OUTPUT_DIRECTORY_RELEASE \"${CMAKE_BINARY_DIR}/bin/release\"\n";
		cmakeCode += u"	LIBRARY_OUTPUT_DIRECTORY_DEBUG \"${CMAKE_BINARY_DIR}/bin/debug\"\n";
		cmakeCode += u"	LIBRARY_OUTPUT_DIRECTORY_RELEASE \"${CMAKE_BINARY_DIR}/bin/release\"\n";
		cmakeCode += u"	ARCHIVE_OUTPUT_DIRECTORY_DEBUG \"${CMAKE_BINARY_DIR}/bin/debug\"\n";
		cmakeCode += u"	ARCHIVE_OUTPUT_DIRECTORY_RELEASE \"${CMAKE_BINARY_DIR}/bin/release\"\n";

		cmakeCode += u"	RUNTIME_OUTPUT_DIRECTORY_DEBUG \"" + debug程序目录 + u"扩展/插件模块/Debug/\"\n";
		cmakeCode += u"	RUNTIME_OUTPUT_DIRECTORY_RELEASE \"" + release程序目录 + u"扩展/插件模块/\"\n";
		//cmakeCode += u"	LIBRARY_OUTPUT_DIRECTORY_DEBUG \"" + 输出目录.u16string() + u"\"\n";
		//cmakeCode += u"	LIBRARY_OUTPUT_DIRECTORY_RELEASE \"" + 输出目录.u16string() + u"\"\n";
		//cmakeCode += u"	ARCHIVE_OUTPUT_DIRECTORY_DEBUG \"" + 输出目录.u16string() + u"\"\n";
		//cmakeCode += u"	ARCHIVE_OUTPUT_DIRECTORY_RELEASE \"" + 输出目录.u16string() + u"\"\n";
		
		//调试
		//cmakeCode += u"	VS_DEBUGGER_WORKING_DIRECTORY \"" + S_引擎配置::g_程序路径.generic_u16string() + u"\"\n";
		//cmakeCode += u"	VS_DEBUGGER_COMMAND \"" + 主程序.generic_u16string() + u"\"\n";
		//cmakeCode += u"	VS_DEBUGGER_COMMAND \"" + 主程序.u16string() + u"\"\n";
		cmakeCode += u")\n";

		
		cmakeCode += u"set_target_properties(${DLL_NAME} PROPERTIES\n";
		cmakeCode += u"	VS_DEBUGGER_WORKING_DIRECTORY\n";
		cmakeCode += u"	$<$<CONFIG:Debug>:" + debug程序目录 + u"扩展/插件模块/>";
		cmakeCode += u"$<$<NOT:$<CONFIG:Debug>>:" + release程序目录 + u"扩展/插件模块/>\n";
		cmakeCode += u")\n";

		auto debug主程序 = debug程序目录 + u"节点3D.exe";
		auto release主程序 = release程序目录 + u"节点3D.exe";
		cmakeCode += u"set_target_properties(${DLL_NAME} PROPERTIES\n";
		cmakeCode += u"	VS_DEBUGGER_COMMAND\n";
		cmakeCode += u"	$<$<CONFIG:Debug>:" + debug主程序 + u">";
		cmakeCode += u"$<$<NOT:$<CONFIG:Debug>>:" + release主程序 + u">\n";
		cmakeCode += u")\n";


		cmakeCode += u"set(CMAKE_LIBRARY_OUTPUT_DIRECTORY \"${CMAKE_BINARY_DIR}/lib\")\n";
		cmakeCode += u"set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY \"${CMAKE_BINARY_DIR}/lib\")\n";

		auto txt = f_str_u16_to_u8(cmakeCode);

		fwrite(txt.data(), sizeof(uint8), txt.size(), f);
		fclose(f);




		std::string cmd = (S_引擎配置::g_程序路径 / "compiled/cmake/bin/cmake.exe").string();
		
		std::string 编译参数;
	#ifdef _DEBUG
		编译参数 = " -DCMAKE_BUILD_TYPE=Debug";
	#else
		编译参数 = " -DCMAKE_BUILD_TYPE=Release";
	#endif // DEBUG
		
		cmd += " -S " + 构建目录.string() + " -B " + (构建目录 / "build").string() + 编译参数;
		

		system(cmd.c_str());
	}
}









